package com.zhm.demo.util;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

public class OkHttpProxyExample {

    public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException {
        // 代理服务器的地址和端口
//        String proxyHost = "10.101.0.94";
//        int proxyPort = 8080;
        String proxyHost = "20.212.170.132";
        int proxyPort = 12125;

        // 配置代理
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));

        // 创建 OkHttpClient 实例，并配置代理
        OkHttpClient client = new OkHttpClient.Builder()
                .proxy(proxy) // 设置代理
//                .sslSocketFactory(OkHttpUtil.getIgnoreInitedSslContext().getSocketFactory(), OkHttpUtil.IGNORE_SSL_TRUST_MANAGER_X509)
//                .hostnameVerifier(OkHttpUtil.getIgnoreSslHostnameVerifier())
                .build();

        // 发送请求
        Request request = new Request.Builder()
                .url("https://www.google.com")
                .build();

        try (Response response = client.newCall(request).execute()) {
            // 处理响应
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            // 例如，打印响应体（注意：实际使用中应检查响应内容类型）
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
